package com.zzz.gp.service.analyse;

import com.zzz.gp.bean.GpHistory;
import com.zzz.gp.dao.GpMapper;
import com.zzz.gp.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZZZ
 * @time: 2022/5/24 20:56
 */
@Slf4j
@Service
public class LPService {
    @Autowired
    private GpMapper gpMapper;
    @Value("${LP}")
    private String path1;

    @Async("myAsync")
    public void init() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today = dtf.format(LocalDateTime.now());
        String path = "/home/" + today + path1;
        LocalDateTime s = LocalDateTime.now();
        List<String> adds = new ArrayList<>();
        int limit = 2;//查询3条
        Map<String, List<GpHistory>> map = new HashMap<>();
        List<GpHistory> result = gpMapper.getHistoryData2(limit);
        result.forEach(x -> {
            if (map.containsKey(x.getCode())) {
                List<GpHistory> his = map.get(x.getCode());
                his.add(x);
            } else {
                List<GpHistory> his = new ArrayList<>();
                his.add(x);
                map.put(x.getCode(), his);
            }
        });
        for (Map.Entry<String, List<GpHistory>> entry : map.entrySet()) {
            String code = entry.getKey();
            List<GpHistory> list = entry.getValue();
            //LP(code, list, adds);
            //LP2(code, list, adds);
            LP5(code, list, adds);
            LP4(code, list, adds);
            LP3(code, list, adds);

        }
        if (adds.size() > 0) {
            StringBuilder sb = new StringBuilder("");
            adds.forEach(x -> {
                sb.append(",").append(x);
            });
            FileUtil.txtWriter(path, sb.substring(1));
        }
        LocalDateTime e = LocalDateTime.now();
        long seconds = Duration.between(s, e).getSeconds();
        log.info("LP分析 用时{}秒,符合数量{}支", seconds, adds.size());
        log.info(adds.toString());
    }

    //1、昨天涨 今天跌且红（低开高走）
    public void LP(String code, List<GpHistory> list, List<String> adds) {
        int n = 3;
        if (list.size() != n) {
            return;
        }

        //昨天涨
        double b = Double.parseDouble(list.get(1).getIncrease_p());
        if (b < 9 && b > -9) {
            return;
        }
        //今天跌
        double a = Double.parseDouble(list.get(0).getIncrease_p());
        if (a > -0.5) {
            return;
        }
        //今天红（低开高走）
        double open = Double.parseDouble(list.get(0).getOpen());
        double close = Double.parseDouble(list.get(0).getClose());
        if (close < open) {
            return;
        }
        adds.add(code);

    }

    //2、前天涨 昨天跌 今天低开高走
    public void LP2(String code, List<GpHistory> list, List<String> adds) {
        int n = 3;
        if (list.size() != n) {
            return;
        }
        //前天涨
        double c = Double.parseDouble(list.get(2).getIncrease_p());
        if (c < 0) {
            return;
        }

        //昨天跌
        double b = Double.parseDouble(list.get(1).getIncrease_p());
        if (b > -1.5) {
            return;
        }
        //今天小涨
        double a = Double.parseDouble(list.get(0).getIncrease_p());
        if (a > 1.5) {
            return;
        }
        //今天红（低开高走）
        double open = Double.parseDouble(list.get(0).getOpen());
        double close = Double.parseDouble(list.get(0).getClose());
        if (close < open) {
            return;
        }
        adds.add(code);

    }

    //3、冲高回落且红
    public void LP3(String code, List<GpHistory> list, List<String> adds) {
        int n = 3;
        if (list.size() != n) {
            return;
        }
        //昨天 前天 不能大涨
        double b = Double.parseDouble(list.get(1).getIncrease_p());
        double c = Double.parseDouble(list.get(2).getIncrease_p());

        //今天红&涨幅不能超过2.0
        double a = Double.parseDouble(list.get(0).getIncrease_p());
        double todayOpen = Double.parseDouble(list.get(0).getOpen());
        double todayClose = Double.parseDouble(list.get(0).getClose());
        double thigh = Double.parseDouble(list.get(0).getHigh());
        if (todayClose > todayOpen && a < 2.0) {
            //以当日开盘价为准 最高涨幅要超过 4%
            double result = (thigh - todayOpen) / todayOpen;
            if (result >= 0.05) {
                //如果 昨天大涨（超过5），今天和昨天的平均涨幅大于昨天涨幅的一半 直接过滤不要
                if (b > 5 && (a + b) > (b * 0.5)) {
                    return;
                }
                adds.add(code);
            }
        }

    }

    //4、N走势
    public void LP4(String code, List<GpHistory> list, List<String> adds) {
        int n = 3;
        if (list.size() != n) {
            return;
        }

        //今天
        double a = Double.parseDouble(list.get(0).getIncrease_p());
        //昨天
        double b = Double.parseDouble(list.get(1).getIncrease_p());
        // 前天
        double c = Double.parseDouble(list.get(2).getIncrease_p());

        //N形图    前天大涨 昨天大跌 今天大涨
        if (c > 3 && b < -4 && a > 4) {
            adds.add(code);
        }
    }


    public void LP5(String code, List<GpHistory> list, List<String> adds) {
        int n = 3;
        if (list.size() != n) {
            return;
        }

        double t_p = Double.parseDouble(list.get(0).getIncrease_p());
        double t_open = Double.parseDouble(list.get(0).getOpen());
        double t_close = Double.parseDouble(list.get(0).getClose());


        double y_p = Double.parseDouble(list.get(1).getIncrease_p());
        double y_open = Double.parseDouble(list.get(1).getOpen());
        double y_close = Double.parseDouble(list.get(1).getClose());

        //昨天 今天 跌且红
        if (t_p< 0 && y_p < 0) {
            if(t_close>t_open && y_close>y_open){
                adds.add(code);
            }
        }

    }
}
